package com.peng.event;

import com.alibaba.fastjson.JSON;
import com.peng.utils.EncryptUtils;
import com.peng.utils.ExportExcelUtil;
import com.peng.utils.StrUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author : pengyf
 * @version : 1.0
 * @createTime : 2022/1/23 11:13
 * @Description : TODO
 */
public class WakBatchExParamEadListener implements ActionListener {

    private String key;
    private String iv;
    private FileDialog fileDialog;
    private JTextArea jTextArea;
    private JTextField jTextField;

    public WakBatchExParamEadListener(String key,String iv,FileDialog fileDialog,JTextArea jTextArea, JTextField jTextField) {
        this.key = key;
        this.iv = iv;
        this.fileDialog = fileDialog;
        this.jTextArea = jTextArea;
        this.jTextField = jTextField;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        JButton jButton = (JButton) e.getSource();
        String text = jTextArea.getText();
        if (text==null || "".equals(text)){
            JOptionPane.showMessageDialog(jButton.getParent(),"请输入需要加密JSON字符串！！","提示", JOptionPane.WARNING_MESSAGE);
            return;
        }

        try {
            JSON.parse(text);
        }catch (Exception e2){
            JOptionPane.showMessageDialog(jButton.getParent(),"加密字符串JSON格式错误！！" + e2.getMessage(),"提示", JOptionPane.WARNING_MESSAGE);
            return;
        }

        try {
            Integer integer = Integer.valueOf(jTextField.getText());
            if (integer > 120000){
                JOptionPane.showMessageDialog(jButton.getParent(),"生成数量不允许超过12W！！","提示", JOptionPane.WARNING_MESSAGE);
                return;
            }

            fileDialog.setVisible(true);
            String dirpath = fileDialog.getDirectory();//获取保存文件路径并保存到字符串中。
            String fileName = fileDialog.getFile();////获取打保存文件名称并保存到字符串中
            if (dirpath == null || fileName == null){ // //判断路径和文件是否为空
                JOptionPane.showMessageDialog(jButton.getParent(),"异常错误请重试！！","提示", JOptionPane.WARNING_MESSAGE);
                return;//空操作
            }
            String path = dirpath + fileName + ".xlsx";
            long s = System.currentTimeMillis();
            List<Integer> indexList = new ArrayList<>();
            int index = 0;
            while (index != -1){
                index = text.indexOf("$", index + 1);
                indexList.add(index);
            }
            indexList = indexList.subList(0,indexList.size() - 1);
            int size = indexList.size();
            List<Map<String,String>> jmMaplist = new ArrayList<>();
            for (int i = 0; i < Integer.valueOf(jTextField.getText()); i++){
                StringBuffer stringBuffer = new StringBuffer(text);
                for (int j = 0; j < size; j++){
                    stringBuffer.replace(indexList.get(j), indexList.get(j) + 1, StrUtils.getRandomString(1));
                }
                Map<String,String> h = new HashMap<>();
                h.put("param", EncryptUtils.aesEncrypt(key,iv,stringBuffer.toString()));
                jmMaplist.add(h);
            }
            String[] headers = {"请求参数#param"};
            ExportExcelUtil.exportMapExcel2007("","微案款加密参数列表",headers,jmMaplist,path);
            JOptionPane.showMessageDialog(jButton.getParent(),"导出完成！导出" + jmMaplist.size() + "条数,耗时：" + (System.currentTimeMillis() - s),"提示", JOptionPane.WARNING_MESSAGE);
        }catch (Exception e2){
            JOptionPane.showMessageDialog(jButton.getParent(),"解析错误！"+e2.getMessage(),"提示", JOptionPane.WARNING_MESSAGE);
            e2.printStackTrace();
        }

    }
}
